<template>
  <div class='search'>
    <f-form ref='form' :action='api.hyorder.saveTuibu' :form-key='this.formItem.orderId' :file-ids='fileIds'>
      <div>
        <f-form-item label='单位名称' placeholder='请选择单位' name='customName' :disabled='true' :required='true' :value.sync='formItem.customName' :trigger-change='true'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择单位'>
            <a-icon type='home' style='cursor:pointer' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='部门' placeholder='请选择部门' name='deptName' :disabled='true' :required='true' :value.sync='formItem.deptName' :trigger-change='true'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择部门'>
            <a-icon type='team' style='cursor:pointer' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='业务员' placeholder='请选择业务员' name='saleManName' :disabled='true' :required='true' :value.sync='formItem.saleManName' :trigger-change='true' v-if='formItem.deptId'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择业务员'>
            <a-icon type='user' style='cursor:pointer' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='发货方式' type='radio' name='sendType' :disabled='true' dataType='number' :data='api.busCommon.sendType' :value.sync='formItem.sendType' />

        <f-form-item label='发货信息' type='other'>
          <f-table ref='orderDrugs' rowKey='orderDetailsId' :tableColumns='orderDetailsColumns' :scroll='{x:true}' @onSuccess='onLoad' :operator='operator' :params='queryParam' :dataSource='drugList' :autoLoad='false' :showPagination='false' :multiple='false'>
               <span slot='salePrice' slot-scope='text, record, index'>
                  <a-input-number :value='text' style='width: 100px' @change='e => editDrug(e, record)' />
              </span>
          </f-table>
        </f-form-item>
      </div>
    </f-form>
  </div>
</template>
<script>
import api from "../../js/api"

import { commonMixins } from "@/core/mixins/commonMixins"

export default {
  name: "TuibuForm",
  props: {
    processInstanceId: {
      type: String,
      default: ""
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  mixins: [commonMixins],
  components: {},
  data() {
    //操作按钮
    let operator = []
    return {
      refs: this.$refs,
      addressType: 1,
      bankList: [],
      //列表初始化查询参数
      queryParam: {},
      params1: {
        customCode: "",
        addressType: 1
      },
      autoLoad: false,
      readOnly: false,
      api: Object.assign(api, this.api),
      isAdd: true,//是否是添加模式
      loaded: false,//是否加载完成
      operator: operator,
      removeIds: [],
      fileIds: [],
      drugList: [],
      orderDetailsColumns: [],
      formItem: this.getFormItem()
    }
  },
  watch: {},
  methods: {
    getFormItem() {
      return {
        orderId: "",
        orderNo: "",
        delivery: "",
        saleManName: "",
        customName: "",
        companyId: null
      }
    },
    getOrderDetailsColumns() {
      let column = [{
        title: "序号",
        width: "60px",
        align: "left",
        fixed: "left",
        dataIndex: "rowIndex",
        customRender: function(text, record, index) {
          return (record.rowIndex !== "合计") ? (parseInt(index) + 1) : record.rowIndex
        }
      }, {
        title: "商品名称",
        align: "center",
        ellipsis: true,
        width: "140px",
        dataIndex: "drugsName"
      }, {
        title: "规格",
        align: "center",
        ellipsis: true,
        width: "140px",
        dataIndex: "drugsSpec"
      }, {
        title: "仓库",
        align: "center",
        width: "120px",
        ellipsis: true,
        dataIndex: "storeName"
      }, {
        title: "批号",
        align: "center",
        width: "90px",
        ellipsis: true,
        dataIndex: "drugLotNo"
      }, {
        title: "数量",
        align: "center",
        width: "60px",
        needTotal: true,
        dataIndex: "saleNum"
      }, {
        title: "单价",
        align: "center",
        width: "60px",
        scopedSlots: { customRender: "salePrice" },
        dataIndex: "salePrice"
      }, {
        title: "金额",
        align: "center",
        width: "80px",
        needTotal: true,
        dataIndex: "saleAmount"
      }, {
        title: "件容",
        align: "center",
        width: "60px",
        dataIndex: "drugJr"
      }, {
        title: "有效期至",
        align: "center",
        width: "100px",
        dataIndex: "expirationDate"
      }, {
        title: "生产企业",
        align: "center",
        width: "130px",
        ellipsis: true,
        dataIndex: "producersName"
      }]
      return column
    },
    //表单赋值
    setData(data) {
      this.$refs.form.resetFields()
      this.formItem = this.getFormItem()
      this.orderDetailsColumns = this.getOrderDetailsColumns()
      if (data && data.orderId) {
        let params = {
          orderId: data.orderId
        }
        this.http.get(this, {
          url: this.api.hyorder.get,
          params: params,
          success: (me, data) => {
            this.formItem = data
            this.formItem.deptType = data.deptType
            data.drugList.forEach((item) => {
              this.drugList.push(item)
            })
            this.$refs["orderDrugs"].refresh(false)
            this.loaded = true
          }
        })

        this.isAdd = false
        this.loaded = true
      }
    },


    /**
     * 商品加载完成
     */
    onLoad(record) {
      if (record.total > 0) {

      }
    },
    /**
     * 编辑商品
     */
    editDrug(value, record) {
      if (value) {
        record.salePrice = value
        record.saleAmount = value * record.saleNum
      }
    },

    /**
     * 文件上传回调
     */
    uploadSuccess(fileUrl, files, fileIds, removeIds) {
      this.fileIds = fileIds
      this.removeIds.push(...removeIds)
    },

    resetForm() {
      this.$refs.form.resetFields()
      this.formItem = this.getFormItem()
      this.$forceUpdate()
    },

    /**
     * 提交表单
     */
    handleSubmit(callback, beforeSubmit) {

      //提交表单(表单中没有的字段需要单独声明提交)
      let drugs = []
      this.drugList.forEach((item) => {
        drugs.push(item)
      })
      this.$refs.form.submit({
        drugList: JSON.stringify(drugs),
        orderId: this.formItem.orderId
      }, callback, () => {
        beforeSubmit && beforeSubmit()
      }, "orderFormJson")
    },
    handleSuccess() {
      this.$refs["orderDrugs"].refresh()
    }
  },
  mounted() {

  },
  created() {
  }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>
/deep/ .ant-table-footer {
  padding: 6px 16px;
}
</style>
